package com.lc.afbx.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;

import com.lc.afbx.R;
import com.lc.afbx.adapters.AdapterJiaMengJiGou;
import com.lc.afbx.conn.GetApiJiaMengJiGou;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshGridView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/5/19.
 */
public class JoinOrganizationActivity extends BaseActivity implements View.OnClickListener {

    private PullToRefreshGridView pullToRefreshGridView;
    private GridView join_organization_gv;
    private List<GetApiJiaMengJiGou.Data> lists = new ArrayList<>();
    private AdapterJiaMengJiGou adapter;

    private GetApiJiaMengJiGou getApiJiaMengJiGou = new GetApiJiaMengJiGou(new AsyCallBack<GetApiJiaMengJiGou.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiJiaMengJiGou.Info info) throws Exception {

            lists.addAll(info.jiaMengData);

            adapter.notifyDataSetChanged();

            setLastUpdateTime();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshGridView.onPullDownRefreshComplete();
            pullToRefreshGridView.onPullUpRefreshComplete();

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_join_organization);

        pullToRefreshGridView = ((PullToRefreshGridView) findViewById(R.id.join_organization_gv));
        pullToRefreshGridView.setScrollLoadEnabled(false);
        pullToRefreshGridView.setPullLoadEnabled(false);
        pullToRefreshGridView.setPullRefreshEnabled(true);

        pullToRefreshGridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {

                lists.clear();
                getApiJiaMengJiGou.execute(JoinOrganizationActivity.this);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {


            }
        });

        join_organization_gv = pullToRefreshGridView.getRefreshableView();

        join_organization_gv.setNumColumns(2);

        adapter = new AdapterJiaMengJiGou(context, lists);

        join_organization_gv.setAdapter(adapter);

        getApiJiaMengJiGou.execute(JoinOrganizationActivity.this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.back:

                finish();

                return;

        }

    }

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshGridView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        SimpleDateFormat mDateFormat = new SimpleDateFormat("HH:mm:ss");
        return mDateFormat.format(new Date(time));
    }
}
